package com.elsa.DataStatistics.statistics;

import com.elsa.DataStatistics.mapper.*;
import com.elsa.DataStatistics.pojo.DayMp;
import com.elsa.DataStatistics.pojo.DayPersonal;
import com.elsa.DataStatistics.pojo.Menu;
import com.elsa.DataStatistics.pojo.MenuOrder;
import com.elsa.DataStatistics.utils.CalculateUtil;
import com.elsa.DataStatistics.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class DayPersonalStatistics {

    @Autowired
    private TimeUtil timeUtil;

    @Autowired
    private DayMapper dayMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DayMpMapper dayMpMapper;

    @Autowired
    private CalculateUtil calculateUtil;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private DayPersonalMapper dayPersonalMapper;

    @Autowired
    private MenuOrderMapper menuOrderMapper;

    public void dayPersonalStatistics(String oId) {

        int dId = timeUtil.getDayTime();

        List<MenuOrder> menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);

        for (int i = 0; i < menuOrderList.size(); i++) {
            DayPersonal dayPersonal = dayPersonalMapper.selectDayPersonalByDIdAndCId(dId, menuOrderList.get(i).getCId());
            if(dayPersonal == null) {
                dayPersonal = new DayPersonal();
                dayPersonal.setDpsId(dayPersonalMapper.selectCount(null) + 1);
                dayPersonal.setDpId(dId);
                dayPersonal.setCId(menuOrderList.get(i).getCId());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                dayPersonal.setDpExpend(menu.getMPrice() * menuOrderList.get(i).getMoNum());
                dayPersonal.setDpCalorie(menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                dayPersonalMapper.insert(dayPersonal);
            } else {
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                dayPersonal.setDpExpend(dayPersonal.getDpExpend() + menu.getMPrice() * menuOrderList.get(i).getMoNum());
                dayPersonal.setDpCalorie(dayPersonal.getDpCalorie() + menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                dayPersonalMapper.updateById(dayPersonal);
            }
        }
    }

}
